package twitter.client;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;

import twitter.client.adapter.TimelineListAdapter;
import twitter.client.bean.TimelineStatus;
import twitter.client.bean.TwitterAccount;
import twitter.client.listener.TweetListOnScrollListener;
import twitter.client.task.GetTimelineTask;
import twitter.client.task.TweetTask;
import twitter.client.util.Consts;
import twitter.client.util.Global;
import twitter.client.util.Utils;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class TimelineActivity extends Activity implements TextWatcher {

	private ListView timeline;
	private EditText postText;
	private TextView tweetPostTextLength;
	private Button tweetExecButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timeline);

		Intent intent = getIntent();

//		LinearLayout layout = (LinearLayout) findViewById(R.id.tweetPanelView);
//		ViewGroup parent = (ViewGroup) layout.getParent();
//		parent.bringChildToFront(layout);

		timeline = (ListView) findViewById(R.id.timelineTweetList);
		timeline.setOnScrollListener(new TweetListOnScrollListener());
//		postText = (EditText) findViewById(R.id.tweetPostText);
//		postText.addTextChangedListener(this);
//		tweetPostTextLength = (TextView) findViewById(R.id.tweetPostTextLength);
//		tweetExecButton = (Button) findViewById(R.id.tweetExecButton);

		int accountIndex = intent.getIntExtra("accountIndex", -1);
		if (accountIndex >= 0) {

			TwitterAccount account = Global.accounts.get(accountIndex);
			Twitter twitter = Global.twitterMap.get(account.getId());
			if (twitter == null) {
				twitter = new TwitterFactory().getInstance();
				AccessToken accessToken = new AccessToken(
						account.getAccessToken(),
						account.getAccessTokenSecret());
				twitter.setOAuthAccessToken(accessToken);
			}

			Global.currentTwitter = twitter;

			// task
			if (Global.timelineTask == null) {
				Global.timelineTask = new GetTimelineTask(timeline);
			}
			GetTimelineTask task = Global.timelineTask;

			AsyncTask.Status status = task.getStatus();

			if (AsyncTask.Status.RUNNING.equals(status)) {
				return;
			}
			if (AsyncTask.Status.FINISHED.equals(status)) {
				task = new GetTimelineTask(timeline);
				Global.timelineTask = task;
			}
			Global.timelineStatus = new TimelineStatus();

			if (timeline.getFooterViewsCount() == 0) {
				// LayoutInflater inflater = (LayoutInflater) this
				// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				// View footer = inflater.inflate(R.layout.footer_loading,
				// null);
				timeline.addFooterView(Global.loadingFooterView, null, false);
			}

			timeline.setAdapter(new TimelineListAdapter(this,
					R.layout.tweet_row, new ArrayList<Status>()));
			task.execute(twitter);
		}
	}

	public void tweetButtonOnClick(View v) {
		Intent intent = new Intent(this, TweetActivity.class);
		startActivity(intent);
		
//		LinearLayout layout = (LinearLayout) findViewById(R.id.tweetPanelView);
//
//		if (layout.getVisibility() == View.INVISIBLE) {
//			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//			layout.setVisibility(View.VISIBLE);
//			postText.requestFocus();
//			imm.showSoftInput(postText, InputMethodManager.SHOW_FORCED);
//		} else {
//			layout.setVisibility(View.INVISIBLE);
//			// imm.hideSoftInputFromWindow(postText.getWindowToken(), 0);
//		}
	}

	public void tweetExecButtonOnClick(View v) {
		String text = postText.getText().toString();
		if (StringUtils.isEmpty(text)) {
			return;
		}
		
//		if (text.length() > Consts.TWITTER_MAX_LENGTH) {
//			return;
//		}
		
		TweetTask task = new TweetTask(this, postText);
		task.execute(Global.currentTwitter);
		
//		try{
//			Global.currentTwitter.updateStatus(text);
//			
//			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//			imm.hideSoftInputFromWindow(postText.getWindowToken(), 0);
//			
//			postText.setText("");
//		}catch(TwitterException e){
//			Utils.showToast(this, e.getErrorMessage());
//		}

//		progress.dismiss();
	}

	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	public void onTextChanged(CharSequence s, int start, int before, int count) {
		int length = Consts.TWITTER_MAX_LENGTH - s.length();
		int color;
		if (length > 0 && length <= Consts.TWITTER_MAX_LENGTH) {
			tweetExecButton.setEnabled(true);
		} else {
//			tweetExecButton.setEnabled(false);
		}

		if (length < 0) {
			color = Color.RED;
		} else {
			color = Color.BLACK;
		}

		tweetPostTextLength.setTextColor(color);
		tweetPostTextLength.setText(String.valueOf(length));
	}

	private void setupPostTextListener(){
		
	}
}
